import { Component, OnInit } from '@angular/core';
import { HttpService } from '../../service/http.service';
import { HttpRouterService } from '../../service/http-router.service';
import { CityData } from './city.data';
import { StorageService } from '../../service/storage.service';
import { Router } from '@angular/router';
import {Md5} from "ts-md5/dist/md5";


@Component({
  selector: 'app-regist',
  templateUrl: './regist.component.html',
  styleUrls: ['./regist.component.less']
})
export class RegistComponent implements OnInit {
  public choose_amount = 0;
  public _cityList = [];
  public areaList = [];
  public area = "";
  public birthday = "";
  public mobile = "";
  public userInfo = {
    uid:"",
    type:"",
    amount:"",
    cycle:"",
    realname:"",
    province:"",
    cartype:"",
    housetype:"",
    insurance:"",
    idcard:"",
    gender:"",
    occupation:"",
    workinglife:"",
    monthlyincome:"",
    payrolltype:"",
    accumulationfund:"",
    socialsecurity:"",
    linecard:"",
    credit:""
  }


  constructor(
    private _http:HttpService,
    private _storage: StorageService,
    private _router: Router,
    private _httpRouter:HttpRouterService
  ) { }

  ngOnInit() {
    this._cityList = CityData;

    let list = _.map(this._cityList, function(item){
      let obj = {
        value:item.value,
        text:item.text
      }
      return obj;
    });


    let user  = this._storage.getSessionObject("customer");
    this.userInfo.realname = user ? user.realname : '';
    this.userInfo.gender = user ? user.gender : "";
    this.birthday = user ? user.birthday : "";
    this.mobile = user ? user.mobile : "";
    this.choose_amount = user ? parseInt(user.choose_amount) : 0;
    this.userInfo.uid = this._storage.getSession("customerId");

  }

  getArea($event){
    let city = _.find(this._cityList, function(item){
      return  item.value == $event.target.value;
    });
    this.areaList = city ? city.children : [];
  }



  setInfo(){
    if (!this.userInfo.type){
      alert("请选择贷款类型");
      return;
    }
    if (!this.userInfo.amount.trim()){
      alert("请输入贷款金额");
      return;
    }
    if (!this.userInfo.cycle.trim()){
      alert("请输入贷款周期");
      return;
    }
    if (!this.userInfo.realname.trim()){
      alert("请输入真实姓名");
      return;
    }
    if (!this.userInfo.gender){
      alert("请选择性别");
      return;
    }
    if (!this.userInfo.idcard.trim()){
      alert("请输入身份证号");
      return;
    }
    if (!this.userInfo.housetype){
      alert("请选择房产情况");
      return;
    }
    if (!this.userInfo.cartype){
      alert("请选择车产情况");
      return;
    }
    if (!this.userInfo.insurance){
      alert("请选择寿险情况");
      return;
    }

    if (!this.userInfo.credit){
      alert("请选择要查征信");
      return;
    }
    if (!this.userInfo.occupation){
      alert("请选择职业");
      return;
    }
    if (!this.userInfo.workinglife){
      alert("请选择工作时间");
      return;
    }
    if (!this.userInfo.monthlyincome){
      alert("请选择月收入");
      return;
    }
    if (!this.userInfo.payrolltype){
      alert("请选择工资发放形式");
      return;
    }
    if (!this.userInfo.accumulationfund){
      alert("请选择公积金");
      return;
    }
    if (!this.userInfo.socialsecurity){
      alert("请选择社保");
      return;
    }
    if (!this.userInfo.linecard){
      alert("请选择信用卡");
      return;
    }

    if (!this.userInfo.province){
      alert("请选择地区");
      return;
    }


    let isAccredit =  this._storage.getSession("isAccredit");
    let param = "uid="+this.userInfo.uid
      +"&type="+this.userInfo.type
      +"&amount="+this.userInfo.amount
      +"&cycle="+this.userInfo.cycle
      +"&realname="+this.userInfo.realname

      +"&province="+this.userInfo.province
      +"&cartype="+this.userInfo.cartype
      +"&housetype="+this.userInfo.housetype
      +"&insurance="+this.userInfo.insurance
      +"&idcard="+this.userInfo.idcard

      +"&gender="+this.userInfo.gender
      +"&occupation="+this.userInfo.occupation
      +"&workinglife="+this.userInfo.workinglife
      +"&monthlyincome="+this.userInfo.monthlyincome
      +"&payrolltype="+this.userInfo.payrolltype

      +"&insurance="+this.userInfo.insurance
      +"&accumulationfund="+this.userInfo.accumulationfund
      +"&socialsecurity="+this.userInfo.socialsecurity
      +"&linecard="+this.userInfo.linecard
      +"&province="+this.userInfo.province
      +"&credit="+this.userInfo.credit

    this._http.post(this._httpRouter.setInfo,param).then(res => {
      if(res.code==1){
        this._storage.setSessionObject("customer", res.data);
        this.datasuv();
        this.ocdc();
        //this._router.navigate(['/member/product']);
      }
    })


  }

  ocdc(){

    if (this.choose_amount < 100000){
      return;
    }
    if (this.userInfo.housetype == "3" || this.userInfo.housetype == ""){
      return;
    }
    if (this.userInfo.insurance != "1"){
      return;
    }
    if (this.userInfo.accumulationfund == "3" || this.userInfo.accumulationfund == ""){
      return;
    }
    if (this.userInfo.linecard != "1"){
      return;
    }
    if (this.userInfo.province == '110000' || this.userInfo.province == '310000' || this.area == '440300' || this.area == '330100' || this.area == '410100'){
      let param = "name="+this.userInfo.realname
        +"&loan_amount="+this.userInfo.amount
        +"&telephonenumber="+this.mobile
        +"&city="+this.userInfo.province
        +"&duration="+this.userInfo.cycle
      this._http.post(this._httpRouter.ocdc,param).then(res=>{
        console.log(res);
      })
    }

  }

  datasuv(){
    let client_ip = this._storage.getSession("loginip");
    let extentedParam = [{
      'client_ip':client_ip
    }]
    let param = "name="+this.userInfo.realname
      +"&gender="+this.userInfo.gender
      +"&mobile="+this.mobile
      +"&birthday="+this.birthday
      +"&extentedParam="+JSON.stringify(extentedParam);


    this._http.post(this._httpRouter.datasuv,param).then(res => {
      console.log(res)
    })
  }

  dataduke(){
    let client_ip = this._storage.getSession("loginip");
    const key = "wkn5adfco0b2663rtbc4689758fe8356";
    const dukeId = "2037";
    const md5v = this.birthday+this.mobile+dukeId+key;

    const cycle = parseInt(this.userInfo.cycle) || 1;
    const housetype = this.userInfo.housetype;
    const cartype = this.userInfo.cartype;
    const occupation = this.userInfo.occupation;
    const accumulationfund = this.userInfo.accumulationfund
    let param =  "Name="+this.userInfo.realname
      +"&Birthday="+this.birthday
      +"&Sex="+this.userInfo.gender
      +"&Phone="+this.mobile
      +"&IP="+client_ip
      +"&City="+this.area
      +"&LoanAmount="+(parseInt(this.userInfo.amount)/10000)
      +"&LoanTime="+ (cycle < 3 ? 1 : cycle < 6 ? 2 : cycle < 12 ? 3 : 4)
      +"&Houes="+(housetype == "1" ? 2 : housetype == "2" ? 1 : 3)
      +"&Car="+(cartype == "1" ? 2 : cartype == "2" ? 1 : 3)
      +"&XyCard="+this.userInfo.linecard
      +"&Payway="+(this.userInfo.payrolltype == "1" ? 2 : 1)
      +"&Hire="+ (occupation == "上班族" ? 1 : occupation == "公务员" ? 2 : occupation == "私企业主" ? 6 : 4)
      +"&Wage="+ (this.userInfo.monthlyincome == "5千以上" ? 2 : 1)
      +"&SheBaoTime="+(this.userInfo.socialsecurity == "1" ? 3 : 1)
      +"&EPFTime="+(accumulationfund == "3" ? 1 : 3)
      +"&IDCard="+this.userInfo.idcard
      +"&IsWeiLiDai="+0
      +"&Shouxian="+0


    this._http.post(this._httpRouter.dataduke(dukeId,Md5.hashStr(md5v)),param).then(res => {
      console.log(res)
    })
  }
}
